package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.EBookChapter;
import com.jilefojing.beans.pojos.base.BasePo;
import com.jilefojing.beans.pojos.enhanced.EnhancedEBookChapter;
import com.jilefojing.beans.pojos.enhanced.EnhancedStatistics;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.query.EBookChapterQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IEBookChapterDao;
import com.jilefojing.service.IEBookChapterService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年09月30日 20时22分10秒
 */
@Service
public class EBookChapterService extends BaseService<EBookChapter> implements IEBookChapterService {
	@Autowired
	private IEBookChapterDao eBookChapterDao;
	@Autowired
	private IStatisticsService statisticsService;

	@Override
	public Pagination<EnhancedEBookChapter> getPagination(long eBookId, Pagination<EnhancedEBookChapter> pagination, UserContext userContext) {
		@SuppressWarnings("unchecked")
		Pagination<EBookChapter> eBookChapterPagination = eBookChapterDao.getPagination(eBookId, pagination.copy());
		pagination.setCount(eBookChapterPagination.getCount());
		if (!Detect.notEmpty(eBookChapterPagination.getItems())) {
			return pagination;
		}
		pagination.setItems(this.convent2Enhanceds(eBookChapterPagination.getItems()));
		this.appendEnhancedCommons(pagination.getItems(), userContext);
		return pagination;
	}

	@Override
	public List<EnhancedEBookChapter> convent2Enhanceds(List<? extends BasePo> pos) {
		@SuppressWarnings("unchecked")
		List<EBookChapter> eBookChapters = (List<EBookChapter>) pos;
		List<EnhancedEBookChapter> enhancedEBookChapters = new ArrayList<EnhancedEBookChapter>();
		for (EBookChapter _eBookChapter : eBookChapters) {
			enhancedEBookChapters.add(new EnhancedEBookChapter(_eBookChapter));
		}

		return enhancedEBookChapters;
	}

	@Override
	public List<EnhancedEBookChapter> getEnhanceds(List<Long> ids, UserContext userContext) {
		List<EBookChapter> eBookChapters = eBookChapterDao.getListByIds(ids);
		if (!Detect.notEmpty(eBookChapters)) {
			return null;
		}
		List<EnhancedEBookChapter> enhancedEBookChapters = this.convent2Enhanceds(eBookChapters);
		this.appendEnhancedCommons(enhancedEBookChapters, userContext);

		return enhancedEBookChapters;
	}

	@Override
	public List<EnhancedEBookChapter> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		@SuppressWarnings("unchecked")
		List<EnhancedEBookChapter> enhancedEBookChapters = (List<EnhancedEBookChapter>) enhancedItems;
		this.appendEnhancedStatistics(enhancedEBookChapters, userContext);
		return enhancedEBookChapters;
	}

	@Override
	public EnhancedEBookChapter appendEnhancedStatistics(EnhancedEBookChapter enhancedEBookChapter, UserContext userContext) {
		this.appendEnhancedStatistics(Arrays.asList(new EnhancedEBookChapter[] { enhancedEBookChapter }), userContext);
		return enhancedEBookChapter;
	}

	@Override
	public List<EnhancedEBookChapter> appendEnhancedStatistics(List<EnhancedEBookChapter> enhancedEBookChapters, UserContext userContext) {

		List<Long> bookIds = new ArrayList<Long>();
		for (EnhancedEBookChapter _EnhancedEBookChapter : enhancedEBookChapters) {
			bookIds.add(_EnhancedEBookChapter.getId());
		}
		List<EnhancedStatistics> enhancedStatisticses = statisticsService.getEnhancedStatisticses(ItemTypeEnumForSource.BOOK_CHAPTER, bookIds, userContext);

		if (Detect.notEmpty(enhancedStatisticses)) {
			for (EnhancedEBookChapter _EnhancedEBookChapter : enhancedEBookChapters) {
				for (EnhancedStatistics _EnhancedStatistics : enhancedStatisticses) {
					if (_EnhancedStatistics.getEnhancedItem() != null) {
						if (_EnhancedEBookChapter.getId() == ((EnhancedEBookChapter) _EnhancedStatistics.getEnhancedItem()).getId()) {
							_EnhancedEBookChapter.setEnhancedStatistics(_EnhancedStatistics);
							break;
						}
					}
				}
			}
		}
		return enhancedEBookChapters;
	}

	@Override
	public EnhancedEBookChapter getEnhancedPrevInEBook(long eBookId, long id, UserContext userContext) {
		EBookChapter eBookChapter = eBookChapterDao.getPrevInEBook(eBookId, id);
		if (eBookChapter == null) {
			return null;
		}
		return (EnhancedEBookChapter) this.convent2Enhanced(eBookChapter);
	}

	@Override
	public EnhancedEBookChapter getEnhancedNextInEBook(long eBookId, long id, UserContext userContext) {
		EBookChapter eBookChapter = eBookChapterDao.getNextInEBook(eBookId, id);
		if (eBookChapter == null) {
			return null;
		}
		return (EnhancedEBookChapter) this.convent2Enhanced(eBookChapter);
	}

	@Override
	public Pagination<EnhancedEBookChapter> getPagination(EBookChapterQuery eBookChapterQuery, Pagination<EnhancedEBookChapter> pagination, UserContext userContext) {
		@SuppressWarnings("unchecked")
		Pagination<EBookChapter> eBookChapterPagination = eBookChapterDao.getPagination(eBookChapterQuery, pagination.copy());
		pagination.setCount(eBookChapterPagination.getCount());
		if (!Detect.notEmpty(eBookChapterPagination.getItems())) {
			return pagination;
		}
		pagination.setItems(this.convent2Enhanceds(eBookChapterPagination.getItems()));
		this.appendEnhancedCommons(pagination.getItems(), userContext);
		return pagination;
	}

}
